#pragma once
#include "IConfigParser.h"
#include "../SimpleIni.h"
#include <map>

class IniParser : public IConfigParser {
public:
    static IniParser& GetInstance();

    // 实现 IConfigParser 接口
    bool LoadFromFile(const std::string& filePath) override;
    bool SaveToFile(const std::string& filePath) override;
    bool SaveToString(std::string& data) override;
    
    // 保持与基类 IConfigParser 一致的返回类型
    const std::vector<std::string>& GetAllSections() const override;
    const std::vector<std::string> GetSectionKeys(const std::string& section) const override;
    std::string GetValue(const std::string& section, const std::string& key, const std::string& defaultValue = "") const override;
    
    bool SetValue(const std::string& section, const std::string& key, const std::string& value) override;
    bool DeleteKey(const std::string& section, const std::string& key, bool removeSectionIfEmpty = false) override;
    bool DeleteSection(const std::string& section) override;

private:
    IniParser() = default;
    virtual ~IniParser() = default;
    IniParser(const IniParser&) = delete;
    IniParser& operator=(const IniParser&) = delete;

    void InitCache();

private:   
    CSimpleIniA m_ini;
    std::vector<std::string> m_sections;
    std::map<std::string, std::vector<std::string> > m_sectionKeys;
    std::map<std::pair<std::string, std::string>, std::string> m_valueCache;
};